package com.aaie.gru.quartz;

import com.aaie.gru.getui.PushtoSingle;
import com.aaie.gru.gruenum.UserType;
import com.aaie.gru.interview.entity.GruSysInterviewDto;
import com.aaie.gru.interview.entity.GruSysInterviewVo;
import com.aaie.gru.interview.service.IGruSysInterviewService;
import com.aaie.gru.user.entity.GruSysUser;
import com.aaie.gru.user.service.IGruSysUserService;
import com.aaie.gru.util.CommonUtil;
import com.aaie.gru.util.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class ScheduledTasks {

    @Autowired
    IGruSysUserService gruSysUserService;

    @Autowired
    IGruSysInterviewService gruSysInterviewService;

    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    /**
     * 每天早上八点执行
     */
    @Scheduled(cron = "0 0 8 * * ? ")
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }

    @Scheduled(cron = "0 32 20 * * ? ")
    public void reportCurrentTime6() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }

    /**
     * 每天早上八点执行 访谈消息推送
     */
    @Scheduled(cron = "0 0 8 * * ? ")
    public void interviewPush() {
        // 获取所有访谈的信息
        GruSysInterviewDto param = new GruSysInterviewDto();
        param.setCurrent(1);
        param.setSize(10000);
        param.setInterviewStatus("是");
        IPage<GruSysInterviewVo> page = gruSysInterviewService.getGruSysInterviewVoList(param);
        List<GruSysInterviewVo> records = page.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            for (GruSysInterviewVo gruSysInterviewVo : records) {
                // 如果预警时间为空 此条数据无作用
                String interviewWarningDate = gruSysInterviewVo.getInterviewWarningDate();
                String interviewDate = gruSysInterviewVo.getInterviewDate();
                if (StringUtils.isNotEmpty(interviewWarningDate) && StringUtils.isNotEmpty(interviewDate)) {
                    // 预警时间格式dd/MM/YYYY
                    long interviewWarningDatel = CommonUtil.date2TimeStamp(interviewWarningDate);
                    long interviewDatel = CommonUtil.date2TimeStamp(interviewDate);
                    Long aLong = CommonUtil.generateCurrentTimeMillis();
                    // 大于预警时间 小于访谈时间才推送，不然推送的时间有可能小于当前时间，无意义
                    if (aLong >= interviewWarningDatel && aLong <= interviewDatel) {
                        // 给学生推送消息
                        PushtoSingle.pushtoSingle("访谈消息", "您的监护人" + gruSysInterviewVo.getGuardianName() + "将于" + gruSysInterviewVo.getInterviewDate() + "对您进行访谈", "{\"bizType\":\"800\"}", gruSysInterviewVo.getStuClientId(), gruSysInterviewVo.getStuPlatform());
                        // 给监护人推送消息
                        PushtoSingle.pushtoSingle("访谈消息", "您将于" + gruSysInterviewVo.getInterviewDate() + "对" + gruSysInterviewVo.getStuName() + "进行访谈", "{\"bizType\":\"700\"}", gruSysInterviewVo.getGuaClientId(), gruSysInterviewVo.getGuaPlatform());
                    }
                }
            }
        }
    }

    /**
     * 服务到期推送
     */
    @Scheduled(cron = "0 0 8 * * ? ")
    public void servicePush() {
        QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", UserType.STUDENT.getValue());
        queryWrapper.eq("stu_service_push_status", "是");
        List<GruSysUser> gruSysUserList = gruSysUserService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(gruSysUserList)) {
            for (GruSysUser gruSysUser : gruSysUserList) {
                String stuServiceEndDate = gruSysUser.getStuServiceEndDate();
                // 服务到期时间和当前时间差
                long daySub = DateUtil.getDaySub(stuServiceEndDate, CommonUtil.generateLocalDateTime());
                if (daySub <= 7 && daySub > 0) {
                    PushtoSingle.pushtoSingle("服务即将到期消息", "您的服务将于" + gruSysUser.getStuServiceEndDate() + "到期，请及时续费，若已缴费请忽略", "{\"bizType\":\"900\"}", gruSysUser.getUserClientId(), gruSysUser.getPlatform());
                }
            }
        }
    }

    /**
     * 服务到期推送
     */
    @Scheduled(cron = "0 0 8 * * ? ")
    public void studentBirthdayPush() {
        // 获取所有监护人
        QueryWrapper<GruSysUser> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("user_type", UserType.STUDENT.getValue());
        List<GruSysUser> guardianList = gruSysUserService.list(queryWrapper2);

        // 获取所有学生
        QueryWrapper<GruSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_type", UserType.STUDENT.getValue());
        List<GruSysUser> studentList = gruSysUserService.list(queryWrapper);

        if (!CollectionUtils.isEmpty(studentList)) {
            for (GruSysUser student : studentList) {
                String stuBirthday = student.getStuBirthday();
                // 服务到期时间和当前时间差
                long daySub = DateUtil.getDaySub(stuBirthday, CommonUtil.generateLocalDateTime());
                // 给监护人推送
                if (daySub == 3) {
                    if (!CollectionUtils.isEmpty(guardianList)) {
                        for (GruSysUser guardian : guardianList) {
                            if (guardian.getUserId().equals(student.getStuGurdianId())) {
                                PushtoSingle.pushtoSingle("生日提醒", "您的学生" + student.getUserName() + "将于三天后过生日，快送去祝福吧！", "{\"bizType\":\"902\"}", guardian.getUserClientId(), guardian.getPlatform());
                                break;
                            }
                        }
                    }
                }
                // 给学生推送
                if (daySub == 0) {
                    PushtoSingle.pushtoSingle("生日提醒", "亲爱的"+student.getUserName()+"您好，祝您生日快乐！", "{\"bizType\":\"901\"}", student.getUserClientId(), student.getPlatform());
                }
            }
        }
    }
}